/*global Element */
(function(window, document) {
    'use strict';

    var keyboardAllowed = typeof Element !== 'undefined' && 'ALLOW_KEYBOARD_INPUT' in Element, // IE6 throws without typeof check

        fn = (function() {
            var val, valLength;
            var fnMap = [
                [
                    'requestFullscreen',
                    'exitFullscreen',
                    'fullscreenElement',
                    'fullscreenEnabled',
                    'fullscreenchange',
                    'fullscreenerror'
                ],
                // new WebKit
                [
                    'webkitRequestFullscreen',
                    'webkitExitFullscreen',
                    'webkitFullscreenElement',
                    'webkitFullscreenEnabled',
                    'webkitfullscreenchange',
                    'webkitfullscreenerror'

                ],
                // old WebKit (Safari 5.1)
                [
                    'webkitRequestFullScreen',
                    'webkitCancelFullScreen',
                    'webkitCurrentFullScreenElement',
                    'webkitCancelFullScreen',
                    'webkitfullscreenchange',
                    'webkitfullscreenerror'

                ],
                [
                    'mozRequestFullScreen',
                    'mozCancelFullScreen',
                    'mozFullScreenElement',
                    'mozFullScreenEnabled',
                    'mozfullscreenchange',
                    'mozfullscreenerror'
                ]
            ];
            var i = 0;
            var l = fnMap.length;
            var ret = {};

            for (; i < l; i++) {
                val = fnMap[i];
                if (val && val[1] in document) {
                    for (i = 0, valLength = val.length; i < valLength; i++) {
                        ret[fnMap[0][i]] = val[i];
                    }
                    return ret;
                }
            }
            return false;
        })(),

        screenfull = {
            request: function(elem) {
                var request = fn.requestFullscreen;

                elem = elem || document.documentElement;

                // Work around Safari 5.1 bug: reports support for
                // keyboard in fullscreen even though it doesn't.
                // Browser sniffing, since the alternative with
                // setTimeout is even worse
                if (/5\.1[\.\d]* Safari/.test(navigator.userAgent)) {
                    elem[request]();
                } else {
                    elem[request](keyboardAllowed && Element.ALLOW_KEYBOARD_INPUT);
                }
            },
            exit: function() {
                document[fn.exitFullscreen]();
            },
            toggle: function( elem ) {
                if (this.isFullscreen) {
                    this.exit();
                } else {
                    this.request(elem);
                }
            },
            onchange: function() {},
            onerror: function() {}
        };

    if (!fn) {
        return window.screenfull = false;
    }

    Object.defineProperties(screenfull, {
        isFullscreen: {
            get: function() {
                return !!document[fn.fullscreenElement];
            }
        },
        element: {
            enumerable: true,
            get: function() {
                return document[fn.fullscreenElement];
            }
        },
        enabled: {
            enumerable: true,
            get: function() {
                // Coerce to boolean in case of old WebKit
                return !!document[fn.fullscreenEnabled];
            }
        }
    });

    document.addEventListener(fn.fullscreenchange, function(e) {
        screenfull.onchange.call(screenfull, e);
    });

    document.addEventListener(fn.fullscreenerror, function(e) {
        screenfull.onerror.call(screenfull, e);
    });

    window.screenfull = screenfull;

})(window, document);